// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	imports_tsusa_74-88.dta
//			tsus_to_new.dta
//			cty_codes2.dta
//			us_imports_hs8.dta
//			mfa_hs8.dta
//			spread_hs8.dta


// outputs:	dataset_tsushts.dta

// Using the TSUSA and HS datasets constructed earlier, this code splices the two 
// to create a panel from 1974--08

set varabbrev off 
clear all 

global dir_int "../20 Intermediate files/"
global dir_raw "../01 Raw data/"

use "${dir_int}imports_tsusa_74-88.dta", clear

drop tar_unwgt units
rename quantity q_jst
rename cusvalue v_jst
rename cifvalue vcif_jst
rename duties duties_jst
rename tsusa TSUS
merge m:1 TSUS using "${dir_int}tsus_to_new.dta"
drop if _merge!=3
drop _merge

gen cty_desc=lower(country)
replace cty_desc="afghanistan" if cty_desc=="afghan"
replace cty_desc="united arab emirates" if cty_desc=="arab_em"
replace cty_desc="argentina" if cty_desc=="argent"
replace cty_desc="australia" if cty_desc=="austral"
replace cty_desc="barbados" if cty_desc=="barbado"
replace cty_desc="belgium" if cty_desc=="bel_lux"
replace cty_desc="bangladesh" if cty_desc=="bngldsh"
replace cty_desc="burkina faso" if cty_desc=="burkina"
replace cty_desc="burma (myanmar)" if cty_desc=="burma"
replace cty_desc="central african republic" if cty_desc=="c_africa"
replace cty_desc="cambodia" if cty_desc=="cambod"
replace cty_desc="costa rica" if cty_desc=="cos_rica"
replace cty_desc="czechoslovakia" if cty_desc=="czecho"
replace cty_desc="dominican republic" if cty_desc=="dom_rep"
replace cty_desc="equatorial guinea" if cty_desc=="eq_gnea"
replace cty_desc="ethiopia (incl. eritrea)" if cty_desc=="ethiopia"
replace cty_desc="falkland islands (islas malvinas)" if cty_desc=="falk_is"
replace cty_desc="french guiana" if cty_desc=="fr_guian"
replace cty_desc="" if cty_desc=="fr_ind_o"
replace cty_desc="guinea-bissau" if cty_desc=="g_bisau"
replace cty_desc="germany" if cty_desc=="german"
replace cty_desc="germany" if cty_desc=="german_e"
replace cty_desc="gibraltar" if cty_desc=="gilbralt"
replace cty_desc="greenland" if cty_desc=="greenld"
replace cty_desc="guadeloupe" if cty_desc=="guadlpe"
replace cty_desc="guatemala" if cty_desc=="guatmala"
replace cty_desc="honduras" if cty_desc=="hondura"
replace cty_desc="hong kong" if cty_desc=="hongkong"
replace cty_desc="indonesia" if cty_desc=="indones"
replace cty_desc="israel (incl. gaza and west bank)" if cty_desc=="israel"
replace cty_desc="cote d'ivoire" if cty_desc=="ivy_cst"
replace cty_desc="jordan" if cty_desc=="jordon"
replace cty_desc="north korea" if cty_desc=="korea_n"
replace cty_desc="south korea" if cty_desc=="korea_s"
replace cty_desc="laos" if cty_desc=="lao"
replace cty_desc="macao" if cty_desc=="macau"
replace cty_desc="madagascar" if cty_desc=="madagas"
replace cty_desc="mauritania" if cty_desc=="mauritn"
replace cty_desc="mongolia" if cty_desc=="mongola"
replace cty_desc="mozambique" if cty_desc=="mozambq"
replace cty_desc="mauritius" if cty_desc=="mritius"
replace cty_desc="netherlands antilles" if cty_desc=="n_antil"
replace cty_desc="netherlands" if cty_desc=="nethlds"
replace cty_desc="new caledonia" if cty_desc=="new_cale"
replace cty_desc="papua new guinea" if cty_desc=="new_guin"
replace cty_desc="new zealand" if cty_desc=="new_zeal"
replace cty_desc="nicaragua" if cty_desc=="nicaraga"
replace cty_desc="paraguay" if cty_desc=="paragua"
replace cty_desc="philippines" if cty_desc=="phil"
replace cty_desc="south africa" if cty_desc=="s_africa"
replace cty_desc="saint helena" if cty_desc=="s_helna"
replace cty_desc="el salvador" if cty_desc=="salvadr"
replace cty_desc="samoa (western)" if cty_desc=="samoa"
replace cty_desc="saudi arabia" if cty_desc=="sd_arab"
replace cty_desc="seychelles" if cty_desc=="seychel"
replace cty_desc="sierra leone" if cty_desc=="sier_ln"
replace cty_desc="singapore" if cty_desc=="singapr"
replace cty_desc="saint pierre and miquelon" if cty_desc=="sp_mqel"
replace cty_desc="sri lanka" if cty_desc=="sri_lka"
replace cty_desc="sudan (incl. south sudan)" if cty_desc=="sudan"
replace cty_desc="saint kitts and nevis" if cty_desc=="st_k_nev"
replace cty_desc="suriname" if cty_desc=="surinam"
replace cty_desc="switzerland" if cty_desc=="switzld"
replace cty_desc="trinidad and tobago" if cty_desc=="trinidad"
replace cty_desc="united kingdom" if cty_desc=="ukingdom"
replace cty_desc="venezuela" if cty_desc=="venez"
replace cty_desc="yemen" if cty_desc=="yemen_n"
replace cty_desc="yemen" if cty_desc=="yemen_s"
replace cty_desc="yugoslavia" if cty_desc=="yugoslav"
replace cty_desc="congo, dem. rep." if cty_desc == "zaire"
drop if cty_desc=="unknown" | cty_desc=="asia_nes" | cty_desc=="us_nes" | cty_desc==""

collapse (sum) *_jst, by(family year cty_desc)
merge m:1 cty_desc using "${dir_raw}cty_codes2.dta"
drop if _merge==2
drop _merge
save "${dir_int}temporary_tsus.dta", replace


// Collapse TSUSA data at new product code
use "${dir_raw}cty_codes2.dta", clear
drop if cty_desc == "zaire"
save "${dir_int}temporary_countries.dta", replace

use "${dir_int}us_imports_hs8.dta", clear
merge m:1 hs8 using "${dir_int}mfa_hs8.dta"
drop if _merge==2
drop _merge
replace phase=0 if phase==.
merge m:1 hs8 year using "${dir_int}spread_hs8.dta"
drop if _merge==2
drop _merge
bysort hs8: egen s2001 = mean(s/(year==2001))
bysort hs8: egen s1999 = mean(s/(year==1999))
bysort hs8: egen smax = max(s)

*** merge some countries (Czech Republic and Slovakia, Belgium and Luxemburg, East and West Germany, Gaza, West bank and Israel, Sudan and South Sudan)
* Yugoslavia: Serbia, Montenegro, Kosovo, Bosnia and Herzogovina, Croatia, Slovenia, Macedonia
* Eritrea and Ethiopia
replace cty_code=4350 if cty_code==4359 | cty_code==4351
replace cty_code=4231 if cty_code==4239
replace cty_code=4280 if cty_code==4290
replace cty_code=5080 if cty_code==5081 | cty_code==5082 | cty_code==5083
replace cty_code=7320 if cty_code==7321 | cty_code==7323
replace cty_code=4790 if cty_code==4799 | cty_code==4791 | cty_code==4792 | cty_code==4793 | cty_code==4794 | cty_code==4801 | cty_code==4802 | cty_code==4803 | cty_code==4804
replace cty_code=7740 if cty_code==7741 | cty_code==7749

merge m:1 cty_code using "${dir_int}temporary_countries.dta"
drop if _merge==2
drop _merge
rename hs8 HTS
merge m:1 HTS using "${dir_int}hts_to_new.dta"
drop if _merge!=3
drop _merge

egen temptag=tag(HTS)
foreach x in s2001 s1999 smax phase nntr {
	bysort family: egen `x'_savg = mean(`x'/(temptag==1))
	bysort family: egen `x'_med = median(`x'/(temptag==1))
} 
collapse (first) cty_desc *_savg *_med (sum) *_jst, by(family year cty_code)
append using "${dir_int}temporary_tsus.dta"

foreach x in s2001 s1999 smax phase nntr {
	bysort family: egen temp = mean(`x'_savg)
	replace `x'_savg = temp if `x'_savg==.
	drop temp
	bysort family: egen temp = mean(`x'_med)
	replace `x'_med = temp if `x'_med==.
	drop temp
}
rename family g

rename cty_code j
gen ntr_j =  j!=4610 & j!=5700 & j!=4850 & j!=4370 & j!=4350 & j!=5520 & j!=5550 & j!=4810 & j!=4870 & j!=5790
* Note: Yugoslavia and poland were exempted from NNTR treatment after 1962, Cuba is not identified
*gen fta =  j==2010 | j==1220
gen fta= (j==5081 & year>1984) | (j==5110 & year>2000) | (j==6021 & year>2004) | (j==5590 & year>2003) ///
| (j==3370 & year>2003) | (j==5250 & year>2005) | (j==7140 & year>2005) | (j==5230 & year>2005) | (j==3330 & year>2007) ///
| (j==2230 & year>2008) | (j==2110 & year>2008) | (j==2050 & year>2008) | (j==2150 & year>2008) ///
| (j==2190 & year>2008) | (j==2470 & year>2008) | (j==5800 & year>2009) ///
| (j==3010 & year>2011) | (j==2250 & year>2011) | (j==5880 & year>2019) | (j==1220 & year>1987) | (j==2010 & year>1993)
gen chn = j==5700
gen viet = j==5520

*** Balanced sample
bysort cty_desc g: egen tempmin = min(year)
gen tempchn = tempmin<=1990 & chn==1
bysort g: egen balanced_chn = max(tempchn)
drop temp*

*** Create some variables
gen lv_jst = log(v_jst)
gen tariff_jst = duties_jst/v_jst
gen ltariff_jst = log(1+tariff_jst)
gen shipping_jst = (vcif_jst-v_jst)/v_jst
replace shipping_jst=0 if shipping_jst<0
gen lshipping_jst = log(1+shipping_jst)
foreach x in 1999_med 1999_savg 2001_med 2001_savg max_med max_savg {
gen lgap`x' = log(1+s`x')
}
gen lnntr_med = log(1+nntr_med)
gen lnntr_savg = log(1+nntr_savg)
bysort g: egen lnntr_chn = mean(ltariff_jst/(year<1980 & chn==1))
bysort g: egen lnntr_allcom = mean(ltariff_jst/(year<1980 & ntr_j==0))
encode g, gen(gn)
gen postWTO= year>2000
egen id = group(j g)

rename *_jst *_jgt
rename year t
order j g gn id t  
drop un_code iso_code charge_jgt count_jgt
compress
save "${dir_int}dataset_tsushts.dta", replace

////////////////////////////////////////////////////////////////////////////////
// Housekeeping 
erase "${dir_int}temporary_countries.dta"
erase "${dir_int}temporary_tsus.dta"

